let password = document.getElementById("password");
let inputs = document.querySelectorAll("input");
let btn = document.querySelector("button");
// 判断
function judge(reg, item, str, text) {
    if (item.value === "") {
        item.parentNode.className = " form-control error";
        item.nextElementSibling.innerHTML = `${text}为必填项`;
    } else {
        if (reg.test(item.value)) {
            item.parentNode.className = "form-control success";
        } else {
            item.parentNode.className = "form-control error";
            item.nextElementSibling.innerHTML = str;
        }
    }
}
btn.addEventListener("click", function (e) {
    e.preventDefault();
    inputs.forEach(function (item) {
        switch (item.id) {
            case "username":
                // 字母开头，5-16，允许字母数字下划线
                let reg1 = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/g;
                let str1 = "格式错误(请输入字母开头5-16位字母、数字、下划线)";
                judge(reg1, item, str1, "用户名");
                break;
            case "email":
                let reg2 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/g;
                let str2 = "格式错误(请输入正确的邮箱地址)";
                judge(reg2, item, str2, "邮箱");
                break;
            case "password":
                // 字母开头，6-10，允许字母数字下划线
                let reg3 = /^[a-zA-Z]\w{5,9}$/g;
                let str3 = "格式错误(请输入字母开头6-10位字母、数字、下划线)";
                judge(reg3, item, str3, "密码");
                break;
            case "password2":
                if (item.value === "") {
                    item.parentNode.className = " form-control error";
                    item.nextElementSibling.innerHTML = "确认密码为必填项";
                } else {
                    if (item.value === password.value) {
                        item.parentNode.className = "form-control success"
                    } else {
                        item.parentNode.className = "form-control error";
                        item.nextElementSibling.innerHTML = "两次密码输入不一致";
                    }
                }
                break;
        }
    })
})